<?php

class SrvzookeeperController extends My_Controller
{
    public function init()
    {
        parent::init();    
    }
    
    /*
     * Zookeeper 获取配置
     * 
     * @request string clusterApps 集群应用名称
     * @object
     */
    function zkConfigConn()
    {
        $zkClusterApps = $_REQUEST['clusterApps'];

        if($zkClusterApps != '')
        {
            $getZkConfig = Config_Param::getClusterAppNamesMatchZkConfig($zkClusterApps);

            $zkInstance = new SrvZookeeper($getZkConfig['cluster_nodes']);

            $zkInstance->instance();

            return $zkInstance;
        }
    }

    /*
     * Zookeeper 获取树节点 生成Treeview插件所需JSON数据 
     * 
     * @request string root
     * @string
     */
    function treejsAction()
    {
        $getParam = $_REQUEST['root'];
        
        $path = $getParam == 'source' ? '/' : $getParam;

        $zkConn = $this->zkConfigConn();

        $result = $zkConn->getChildren($path);

        if(!empty($result))
        {
            foreach($result as $k => $v)
            {
                $id = $title = $path == '/' ? $path.$v : $path.'/'.$v;

                //常用特殊配置进行高亮显示
                if(strstr($id,'schema.xml'))
                {
                    $title = "<font color='#0072E3'>".$id."</font>";
                }

                $data[$k]['text'] = '<a href="javascript:" value="'.$id.'" onclick="getClickPath(this)">'.$title."</a>";
                $data[$k]['complete'] = true;
                $data[$k]['classes'] = 'folder';
                $getChildren = $zkConn->getChildren($id);

                if(!empty($getChildren))
                {
                    $data[$k]['id'] = $id;
                    $data[$k]['hasChildren'] = true;
                }
                else
                {
                    $data[$k]['classes'] = 'file';
                }
            }
        }
        
        echo json_encode($data);
    }

    function readznodecontentajaxAction()
    {
        $znode = $_GET['znode'];

        if(strstr($znode,"."))
        {
            //'.'文件的位置
            $markPos = strrpos($znode,'.') + 1;

            //获取节点资源后缀
            $suffix = substr($znode,$markPos);

            switch($suffix)
            {
                // case 'xml':
                //     $resourceType = 'text/xml';
                //     break;

                // case 'json':
                //     $resourceType = 'text/json';
                //     break;
                
                default:
                    $resourceType = 'text/plain';
            }

        }
        else
        {
            $resourceType = 'text/plain';
        }


        if($znode != '')
        {
            $zkConn = $this->zkConfigConn();

            if($zkConn->exists($znode))
            {
                $this->_response->setHeader('Content-Type', $resourceType.'; charset=utf-8'); 

                echo $zkConn->get($znode);
            }
        }
    }

    /*
     * 管理 Zookeeper Znode 
     * 
     * @post string selectZkPath 选择的Znode路径
     * @post string submitUpload 上传提交按钮
     * @request string clusterApps 集群应用名称
     * @string
     */
    function adminznodeAction()
    {
        $zkPath = $_POST['selectZkPath'];

        $zkClusterApps = $_REQUEST['clusterApps'];
        
        $getZkConfigList = Config_Param::getClusterZkConfig();

        if(isset($_POST['submitUpload']))
        {
            if($zkPath != '')
            {
                $zkConn = $this->zkConfigConn();

                if($zkConn->exists($zkPath))
                {
                    $checkIsZkFile = $zkConn->getChildren($zkPath);
    
                    if(empty($checkIsZkFile))
                    {
                        $uploadInfo = UtilUploadFile::upload('selectLocalFile','');

                        if($uploadInfo['file_upload_status'] == 0)
                        {
                            if($uploadInfo['file_size'] != 0)
                            {
                                $readFile = file_get_contents($uploadInfo['file_path']);
            
                                if(is_string($readFile) && $readFile != '')
                                {
                                    $msgCode = $zkConn->set($zkPath,$readFile) == true ? '005' : '-005';
                                }
                            }
                            else
                            {
                                $msgCode = '-004';
                            }
                        }
                        else
                        {
                            $msgCode = '-005'; 
                        }
                    }
                    else
                    {
                        $msgCode = '-014';
                    }
                }
                else
                {
                    $msgCode = '-013';
                }
            }
            else
            {
                $msgCode = '-011';
            }
    
            if($msgCode != '')
            {
                echo_alert(UtilMsg::alert($msgCode));exit;
            }
        }

        $hash['clusterApps'] = $_REQUEST['clusterApps'];
        $hash['getZkConfigList'] = $getZkConfigList;
        $this->display($hash,'srvzookeeper/adminznode.tpl');
    }
}

?>
